/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package heuristic;

import core.Node;
import expression.IntExpr;
import expression.Variable;

/**
 *
 * @author ddoose
 */
public class ValueHeuristic implements Heuristic {
    private Variable variable;
    private int value;
    
    public ValueHeuristic() {
        variable = null;
        value = 0;
    }

    public boolean select(Node node) {
        
        int size = Integer.MAX_VALUE;
        // determine la variable avec le domaine le plus petit
        for (Variable v: node.getVariables()) {
            if (v.getCurrent().isLeftBounded() && !v.getCurrent().isValue()) {
                int temp = v.getCurrent().size();
                if (temp < size) {
                    size = temp;
                    variable = v;
                }
            }
        }
        if (variable == null) {
            return false;
        }
        
        value = variable.getCurrent().getLeftBound();
        
        return true;
    }
    
    public void nextConstraint(Node n) {
        IntExpr cst = n.createEq(variable, n.createValue(value));
        n.addConstraint(cst);
    }
    
    public void backtrackConstraint(Node n) {
        IntExpr cst = n.createNeq(variable, n.createValue(value));
        n.addConstraint(cst);
    }
    
    public String toString() {
        return variable + " " + value;
    }
}
